package work20241225;

/**
 * @author zdl
 * @date 2024/12/25
 */
class Account {
    private String num;
    private double money;
    private String name;

    public Account() {
    }

    public Account(String num, double money, String name) {
        this.num = num;
        this.money = money;
        this.name = name;
    }

    /**
     * 获取
     *
     * @return num
     */
    public String getNum() {
        return num;
    }

    /**
     * 设置
     *
     * @param num
     */
    public void setNum(String num) {
        this.num = num;
    }

    /**
     * 获取
     *
     * @return money
     */
    public double getMoney() {
        return money;
    }

    /**
     * 设置
     *
     * @param money
     */
    public void setMoney(double money) {
        this.money = money;
    }

    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return "Account{num = " + num + ", money = " + money + ", name = " + name + "}";
    }
}

class ATM implements Runnable {
    private Account account;
    private double money;


    public ATM(Account account, double money) {
        this.account = account;
        this.money = money;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (account) {
                if (money <= account.getMoney()) {
                    System.out.println(Thread.currentThread().getName() + "线程正在取" + money + "元");
                    account.setMoney(account.getMoney() - money);
                    System.out.println(Thread.currentThread().getName() + "剩余余额为:" + account.getMoney());
                } else {
                    System.out.println(Thread.currentThread().getName() + "余额不足！");
                    break;
                }
            }
        }
    }


}

public class Demo2 {
    public static void main(String[] args) {
        Account account = new Account("1111", 1000, "张三");
        ATM atm1 = new ATM(account, 1);
        ATM atm2 = new ATM(account, 2);
        new Thread(atm1, "线程1:").start();
        new Thread(atm2, "线程2:").start();

    }
}
